package com.zy.experience_system.service.impl;

import com.zy.experience_system.dto.StuAndSetDTO;
import com.zy.experience_system.entity.*;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.ClassAndStuFrom;
import com.zy.experience_system.form.StuForm;
import com.zy.experience_system.repository.*;
import com.zy.experience_system.service.StuService;
import com.zy.experience_system.util.MyDateUtil;
import com.zy.experience_system.util.MyStringUtil;
import com.zy.experience_system.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StuServiceImpl implements StuService {

    @Autowired
    private StuInformationRepository stuInformationRepository;
    @Autowired
    private TestSetRepository testSetRepository;
    @Autowired
    private StuAndSetRepository stuAndSetRepository;
    @Autowired
    private StuAndClassRepository stuAndClassRepository;
    @Autowired
    private ClassInformationRepository classInformationRepository;
    @Autowired
    private ClassAndSetRepository classAndSetRepository;


    @Override
    public StuDataVO findStuDataVO(String stuId) {
        StuDataVO stuDataVO = new StuDataVO();
        StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
        StuInfoVO stuInfoVO = new StuInfoVO();
        BeanUtils.copyProperties(stuInformation, stuInfoVO);
        stuDataVO.setStuInfoVO(stuInfoVO);
        List<StuAndSet> stuAndSetList = stuAndSetRepository.findAllByStuId(stuId);
        List<StuSetVO> stuSetVOList = new ArrayList<>();
        for (StuAndSet stuAndSet : stuAndSetList) {
            TestSet testSet = testSetRepository.findById(stuAndSet.getSetId()).get();
            StuSetVO stuSetVO = new StuSetVO();
            stuSetVO.setSetId(testSet.getSetId());
            stuSetVO.setSetName(testSet.getSetName());
            stuSetVO.setTeachId(testSet.getTeachId());
            stuSetVO.setTeachName(testSet.getTeachName());
            stuSetVO.setStartTime(MyDateUtil.date2String(testSet.getStartTime()));
            stuSetVO.setOverTime(MyDateUtil.date2String(testSet.getOverTime()));
            stuSetVOList.add(stuSetVO);
        }

        stuDataVO.setStuSetVOList(stuSetVOList);

        return stuDataVO;
    }


    @Override
    public List<StuInformation> findAll() {
        List<StuInformation> list = stuInformationRepository.findAll();
        return list;
    }

    @Override
    public void save(StuForm stuForm) {
        StuInformation stuInformation;
        if (stuForm.getId() == null) {
            stuInformation = new StuInformation();
        } else {
            stuInformation = stuInformationRepository.findById(stuForm.getId()).get();
        }
        stuInformation.setStuId(stuForm.getUsername());
        stuInformation.setStuName(stuForm.getName());
        stuInformationRepository.save(stuInformation);
    }


    @Override
    public StuAndClassVO findAllByClassId(Integer classId) {
        List<StuAndClass> stuAndClassList = stuAndClassRepository.findAllByClassId(classId);
        ClassInformation classInformation = classInformationRepository.findById(classId).get();
        ClassInfoVO classInfoVO = new ClassInfoVO();
        classInfoVO.setName(classInformation.getClassName());
        classInfoVO.setTeacher(classInformation.getTeachName());
        StuAndClassVO stuAndClassVO = new StuAndClassVO();
        stuAndClassVO.setClassInfoVO(classInfoVO);
        List<StuInfoVO> stuInfoVOList = new ArrayList<>();
        for (StuAndClass stuAndClass : stuAndClassList) {
            String stuId = stuAndClass.getStuId();
            StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
            StuInfoVO stuInfoVO = new StuInfoVO();
            BeanUtils.copyProperties(stuInformation, stuInfoVO);
            stuInfoVOList.add(stuInfoVO);
        }
        stuAndClassVO.setStuInfoVOList(stuInfoVOList);
        return stuAndClassVO;
    }

    @Override
    public void saveStuAndSet(StuAndSetDTO stuAndSetDTO) {
        if (stuAndSetDTO.getId() == null) {
            StuAndSet stuAndSet = new StuAndSet();
            BeanUtils.copyProperties(stuAndSetDTO, stuAndSet);
            stuAndSetRepository.save(stuAndSet);
        }
    }

    @Override
    public void saveStuAndClass(String students, Integer classId) {
        Map<String, String> stuMap = MyStringUtil.parse(students);
        for (String username : stuMap.keySet()) {
            String stuName = stuMap.get(username);
            if (stuInformationRepository.findByStuId(username) == null) {
                continue;
            } else if (!stuInformationRepository.findByStuId(username).getStuName().equals(stuName)) {
                continue;
            } else if (stuAndClassRepository.findByClassIdAndStuId(classId, username) != null) {
                continue;
            }
            StuAndClass stuAndClass = new StuAndClass();
            stuAndClass.setStuId(username);
            stuAndClass.setClassId(classId);
            stuAndClassRepository.save(stuAndClass);
            StuAndSetDTO stuAndSetDTO = new StuAndSetDTO();
            stuAndSetDTO.setStuId(username);
            List<ClassAndSet> classAndSetList = classAndSetRepository.findAllByClassId(classId);
            for (ClassAndSet classAndSet : classAndSetList) {
                Integer setId = classAndSet.getSetId();
                stuAndSetDTO.setSetId(setId);
                if (stuAndSetRepository.findAllByStuIdAndSetId(stuAndSetDTO.getStuId(),setId).size()==0){
                    saveStuAndSet(stuAndSetDTO);
                }
            }
        }
    }

    @Override
    public void deleteStuAndSet(StuAndSetDTO stuAndSetDTO) {
        Integer setId = stuAndSetDTO.getSetId();
        String stuId = stuAndSetDTO.getStuId();
        stuAndSetRepository.deleteBySetIdAndStuId(setId, stuId);
    }


    @Override
    public void deleteStuAndClass(ClassAndStuFrom classAndStuFrom) {
        Integer classId = classAndStuFrom.getClassId();
        String stuId = classAndStuFrom.getStuId();
        if (stuAndClassRepository.findByClassIdAndStuId(classId, stuId) == null) {
            return;
        }

        stuAndClassRepository.deleteByClassIdAndStuId(classId, stuId);

        List<StuAndSet> stuAndSetList = stuAndSetRepository.findAllByStuId(stuId);
        for (StuAndSet stuAndSet : stuAndSetList) {
            StuAndSetDTO stuAndSetDTO = new StuAndSetDTO();
            stuAndSetDTO.setStuId(stuId);
            stuAndSetDTO.setSetId(stuAndSet.getSetId());
            deleteStuAndSet(stuAndSetDTO);
        }
    }

    @Override
    public List<StuInfoVO> findAllStuFromSet(Integer setId) {
        List<StuAndSet> stuAndSetList = stuAndSetRepository.findAllBySetId(setId);
        if (stuAndSetList != null) {
            List<StuInfoVO> stuInfoVOList = new ArrayList<>();
            for (StuAndSet stuAndSet : stuAndSetList) {
                String stuId = stuAndSet.getStuId();
                StuInformation stuInformation = stuInformationRepository.findByStuId(stuId);
                String name = stuInformation.getStuName();
                StuInfoVO stuInfoVO = new StuInfoVO();
                stuInfoVO.setStuId(stuId);
                stuInfoVO.setStuName(name);
                stuInfoVOList.add(stuInfoVO);
            }
            return stuInfoVOList;
        }
        throw new CommonException(ResultEnum.FAIL);
    }
}
